package org.hongqi.flowable.repository;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/repository")
@RequiredArgsConstructor
@Slf4j
public class RepositoryController {

    private final RepositoryService repositoryService;

    @GetMapping("/listWithoutPage")
    public Collection<RepositoryDTO> listWithoutPage() {
        List<RepositoryDTO> repositoryDTOS = new ArrayList<>();
        log.info("----------开始查询----------");
//        List<Deployment> deployments = repositoryService.createDeploymentQuery().latest().list();
        List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().latestVersion().list();
        processDefinitions.forEach(definition -> {
            Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(definition.getDeploymentId()).singleResult();
            RepositoryDTO repositoryDTO = RepositoryDTO.builder()//
                    .deploymentId(deployment.getId())//
                    .deploymentName(deployment.getName())//
                    .deploymentCategory(deployment.getCategory())//
                    .deploymentKey(deployment.getKey())//
                    .deploymentTenantId(deployment.getTenantId())//
                    .deploymentTime(deployment.getDeploymentTime())//
                    .deploymentParentId(deployment.getParentDeploymentId())//
                    .processDefinitionId(definition.getId())//
                    .processDefinitionKey(definition.getKey())//
                    .processDefinitionCategory(definition.getCategory())//
                    .processDefinitionName(definition.getName())//
                    .processDefinitionVersion(definition.getVersion())//
                    .processDefinitionResourceName(definition.getResourceName())//
                    .processDefinitionDGRMResourceName(definition.getDiagramResourceName())//
                    .processDefinitionDescription(definition.getDescription())//
                    .build();
            repositoryDTOS.add(repositoryDTO);
        });


//        deployments.forEach(deployment -> {
//            ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//            RepositoryDTO repositoryDTO = RepositoryDTO.builder()//
//                    .deploymentId(deployment.getId())//
//                    .deploymentName(deployment.getName())//
//                    .deploymentCategory(deployment.getCategory())//
//                    .deploymentKey(deployment.getKey())//
//                    .deploymentTenantId(deployment.getTenantId())//
//                    .deploymentTime(deployment.getDeploymentTime())//
//                    .deploymentParentId(deployment.getParentDeploymentId())//
//                    .processDefinitionId(definition.getId())//
//                    .processDefinitionKey(definition.getKey())//
//                    .processDefinitionCategory(definition.getCategory())//
//                    .processDefinitionName(definition.getName())//
//                    .processDefinitionVersion(definition.getVersion())//
//                    .processDefinitionResourceName(definition.getResourceName())//
//                    .processDefinitionDGRMResourceName(definition.getDiagramResourceName())//
//                    .processDefinitionDescription(definition.getDescription())
//
//
//                    //
//                    .build();
//            repositoryDTOS.add(repositoryDTO);
//        });
        return repositoryDTOS;
    }

    @PostMapping("/upload")
    public void insert(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        log.info("开始上传文件");
        Deployment deployment = repositoryService.createDeployment().addInputStream(file.getOriginalFilename(), file.getInputStream()).name("leaveName").category("leaveCategory").key("leaveKey").deploy();
        System.out.println(deployment.getDeploymentTime());
    }


}
